<?php

namespace app\backend\controller;

use app\common\model\Message as messageModel;
use app\common\model\Notice;
use app\common\model\User;
use think\Controller;
use think\Validate;
use think\Request;
use think\facade\Env;

class Message extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function messageList()
    {
        $limit = request()->param('limit');
        $find = request()->param('find');
        if($find){
            $list = messageModel::alias('m')->Join('langjin_user u','m.userid = u.id')->where('userid',$find)->where(['m.type'=>0,'m.to_id'=>0])->whereOr('u.nickname','like',"%{$find}%")->whereOr('u.mobile',$find)->field('m.id,m.userid,m.create_time,u.mobile,u.nickname')->order('m.id desc')->paginate($limit);
            $count = messageModel::where('userid',$find)->where(['type'=>0,'to_id'=>0])->count();
        }else{
            $list = messageModel::alias('m')->Join('langjin_user u','m.userid = u.id')->where(['m.type'=>0,'m.to_id'=>0])->field('m.id,m.userid,m.create_time,u.mobile,u.nickname')->order('m.id desc')->paginate($limit);
            $count = messageModel::where(['type'=>0,'to_id'=>0])->count();
        }
        /*$status = ['未回复','已回复'];
        foreach($list as $key=>$value){
            $list[$key]['status1'] = $status[$list[$key]['status']];
        }*/
        if ($list) {
            $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => $list->items()];
            return json($data);
        } else {
            $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => array()];
            return json($data);
        }
    }
    public function reply(){
        $id = request()->param('id');
        $info = messageModel::where('id',$id)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function show_reply(){
        $id = request()->param('id');
        $info = messageModel::where('id',$id)->find();
        $reply_message = messageModel::where(['to_id'=>$id,'type'=>1])->value('content');
        $this->assign('info',$info);
        $this->assign('reply_message',$reply_message);
        return $this->fetch();
    }
    public function sub(){
        if(request()->isPost()){
            $info = $_POST;
            if(!$info['content']){
                return array('code'=>0,'msg'=>'回复内容不能为空');
            }
            $info['to_id'] = $info['id'];
            //改变这条留言的回复状态
            $res = messageModel::where('id',$info['id'])->update(['status'=>1]);
            if($res){
                unset($info['id']);
                $info['type']= 1;
                $info['status']= 1;
                $result = messageModel::create($info);
                if($result){
                    return array('code'=>1,'msg'=>'回复成功');
                }else{
                    return array('code'=>0,'msg'=>'操作失败');
                }
            }

        }
    }
    public function raiders(){
        if(request()->isPost()){
            $res = Notice::where('id',1)->update(['content'=>$_POST['content']]);
            //echo Notice::getLastSql();exit;
            if($res){
                return array('code'=>1,'msg'=>'操作成功');
            }else{
                return array('code'=>0,'msg'=>'操作失败');
            }
        }
        $info  = Notice::where('id',1)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function my_team(){
        if(request()->isPost()){
            $res = Notice::where('id',2)->update(['content'=>$_POST['content']]);
            //echo Notice::getLastSql();exit;
            if($res){
                return array('code'=>1,'msg'=>'操作成功');
            }else{
                return array('code'=>0,'msg'=>'操作失败');
            }
        }
        $info  = Notice::where('id',2)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
}
